package com.contacts.service.Impl;
import com.contacts.mapper.UserMapper;
import com.contacts.pojo.Contact;
import com.contacts.pojo.User;
import com.contacts.result.PageResult;
import com.contacts.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserServiceimpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        User loginuser = userMapper.getByUserphoneAndUserpassword(user);
        return loginuser;
    }

    /**
     * 添加新用户
     * @param user
     */
    @Override
    public void addUser(User user) {
        userMapper.insertUser(user);
    }

    /**
     * 联系人分页查询
     * @param category
     * @param name
     * @param page
     * @param pageSize
     * @param userId
     * @return
     */
    @Override
    public PageResult page(String category, String name, Integer page, Integer pageSize,Integer userId) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //执行条件分页查询
        List<Contact> contactList = userMapper.list(category,name,userId);
        //获取查询结果
        Page<Contact> p =(Page<Contact>) contactList;
        //封装PageResult
        PageResult pageResult = new PageResult(p.getTotal(),p.getResult());
        return pageResult;
    }
}
